package moodish.server.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import moodish.comm.ServerSideMessage.Type;
import moodish.message.MessageFromClient;
import moodish.server.User;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

public class MoodishServerImplTests
{
	private static ServerCommDummyTest		commDummy	= new ServerCommDummyTest();
	private static Thread					serverThread;
	private static MoodishServerImplTesting	server;
	@BeforeClass
	public static void setUp() throws Exception
	{
		commDummy = new ServerCommDummyTest();
		server = new MoodishServerImplTesting();
		serverThread = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				server.start(commDummy);
			}
		});
		serverThread.start();
	}

	@Test
	public void testFirstUserConnection() throws InterruptedException
	{
		MessageFromClient request = new MessageFromClient(Type.CLIENT_CONNECTED, "testUser", null);
		commDummy.addMessage(request);
		Thread.sleep(10);
		assertNotNull(server.connectUsers.get("testUser"));
	}
	@Test
	public void testSecondUserConnection() throws InterruptedException
	{
		MessageFromClient request = new MessageFromClient(Type.CLIENT_CONNECTED, "testUser2", null);
		commDummy.addMessage(request);
		Thread.sleep(10);
		assertNotNull(server.connectUsers.get("testUser2"));
	}
	@Test
	public void testFriendship() throws InterruptedException
	{
		MessageFromClient request = new MessageFromClient(Type.FRIENDSHIP, "testUser", "testUser2");
		commDummy.addMessage(request);
		Thread.sleep(500);
		User user1 = server.connectUsers.get("testUser");
		User user2 = server.connectUsers.get("testUser2");
		assertNotNull(user1);
		assertNotNull(user2);
		assertEquals(user1.isFriend("testUser2"), true);
		assertEquals(user2.isFriend("testUser"), true);
	}
	@Test
	public void sendMoodToFriend() throws InterruptedException
	{
		MessageFromClient request = new MessageFromClient(Type.MOODISH_MESSAGE, "testUser", "MoodTest");
		commDummy.addMessage(request);
		Thread.sleep(100);
		User user1 = server.connectUsers.get("testUser");
		assertNotNull(user1);
		assertEquals(user1.getMood(), "MoodTest");

	}
	@Test
	public void testUnfriendship() throws InterruptedException
	{
		MessageFromClient request = new MessageFromClient(Type.UNFRIENDSHIP, "testUser", "testUser2");
		commDummy.addMessage(request);
		Thread.sleep(100);
		User user1 = server.connectUsers.get("testUser");
		User user2 = server.connectUsers.get("testUser2");
		assertNotNull(user1);
		assertNotNull(user2);
		assertEquals(user1.isFriend("testUser2"), false);
	}
	@Test
	public void testUserDisconnection() throws InterruptedException
	{
		// Connect User
		MessageFromClient request = new MessageFromClient(Type.CLIENT_CONNECTED, "userTODC", null);
		commDummy.addMessage(request);
		Thread.sleep(100);
		assertNotNull(server.connectUsers.get("userTODC"));
		// Disconnect user

		MessageFromClient request2 = new MessageFromClient(Type.CLIENT_DISCONNECTED, "userTODC", null);
		commDummy.addMessage(request2);
		Thread.sleep(100);
		assertEquals(server.connectUsers.get("userTODC"), null);
	}

	@After
	public void dispose()
	{
		serverThread.interrupt();
	}

}
